from mutagen.easyid3 import EasyID3

import id3reader
import solr

import os, shutil, string

class Organize():

    def __init__(self, src_dir, dest_dir):
        self.src_dir = src_dir
        self.dest_dir = dest_dir
        
    def organize(self):
        #shutil.rmtree(self.dest_dir, True)
        try:
            os.mkdir(self.dest_dir)
        except:
            print 'media directory already exists'
        
        self._organize(self.src_dir)
        self.optimize_index()

    def _organize(self, dir):
        for file_or_dir in os.listdir(dir):
            ext = os.path.splitext(file_or_dir)[1]

            path = os.path.join(dir, file_or_dir)
            if file_or_dir != 'media' and os.path.isdir(path):
                self._organize(path)
            elif ext == '.mp3':
                self._rename_and_index(path)

    def _rename_and_index(self, path):
        slr = solr.SolrConnection('http://localhost:8983/solr')

        filename = self._add_to_index(slr, path)

        shutil.copyfile(path, os.path.join(self.dest_dir, filename))

        slr.commit()
        
    def reindex(self):
        slr = solr.SolrConnection('http://localhost:8983/solr')

        i = 0
        
        for file in os.listdir(self.dest_dir):
            ext = os.path.splitext(file)[1]

            path = os.path.join(self.dest_dir, file)
            if ext == '.mp3':
                self._add_to_index(slr, path)
                i+=1

        slr.optimize()
        slr.commit()

        return i
        
    def _add_to_index(self, slr, path):
        id3r = id3reader.Reader(path)

        title = id3r.getValue('title')
        performer = id3r.getValue('performer')
        album = id3r.getValue('album')
        genre = id3r.getValue('genre')

        filename = self._get_filename(id3r) + '.mp3'

        audio = EasyID3(path)
        try:
            genre = audio["genre"][0]
        except:
            print path

        slr.add(id = filename, title = title, author = performer, performer = performer, album = album, filename = filename, genre = genre)

        print filename + ' indexed'

        return filename

    def _get_filename(self, id3r):
        title = id3r.getValue('title')
        performer = id3r.getValue('performer')
        album = id3r.getValue('album')
        
        if (id3r.getValue('track')):
            track = string.replace(id3r.getValue('track'), '/', ' of ')
            s = performer + ' ' + album + ' ' + track
        else:
            s = performer + ' ' + album

        s = ''.join(ch for ch in s if ch not in set(string.punctuation))
        s = string.replace(s, ' ', '-').lower()
        s = string.replace(s, '--', '-')
        
        return s

    def optimize_index(self):
        slr = solr.SolrConnection('http://localhost:8983/solr')
        slr.optimize()
        slr.commit()

    def reset_index(self):
        slr = solr.SolrConnection('http://localhost:8983/solr')
        slr.delete_query('*:*')
        slr.commit()
